package com.itic.appbase.applications.sys.resource.service;

import com.itic.appbase.applications.cacheutils.ResourceCache;
import com.itic.appbase.applications.sys.relation.persistence.dao.RoleResourceDao;
import com.itic.appbase.applications.sys.relation.persistence.model.RoleResourceExample;
import com.itic.appbase.applications.sys.resource.persistence.dao.ResourceDao;
import com.itic.appbase.applications.sys.resource.persistence.model.Resource;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceExample;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceExample.Criteria;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceStat;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ResourceService
        extends CrudService<ResourceDao, Resource, ResourceExample> {
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private RoleResourceDao roleResourceDao;

    /**
     * 覆盖父类方法，增加缓存操作
     */
    @Override
    public String edit(Resource resource) {
        String result = super.edit(resource);
        // 清除缓存
        ResourceCache.remove(resource.getUrl());
        return result;
    }

    /**
     * 按类型统计
     * 
     * @return
     */
    public List<ResourceStat> statByType(Resource resource) {
    	
        return resourceDao.statByType(resource);
    }

    /**
     * 级联删除
     * 
     * @param id
     * @return
     */
    public String deleteTree(String id) {
        int code = -1;
        Resource resource = new Resource();
        resource.setId(id);
        resource.setIsEffective(DBConst.TRUE);
        
        String isMysql = PropertyGetter.getString("app.db.type");
    	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
    		//先删除本级的ID
    		Resource one = resourceDao.selectByPrimaryKey(id);
    		if( null != one ){
    			code += resourceDao.deleteByPrimaryKey(id);
    			//删除下级
    			ResourceExample rExample = new ResourceExample();// 逻辑删除资源信息
    			Criteria criteria = rExample.createCriteria();
    			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    			criteria.andPIdEqualTo(id);
    			code += resourceDao.deleteByExample(rExample);
    		}
        }else{
        	
        	List<String> ids = resourceDao.selectResourceIdList(resource);// 获得要删除的资源树
        	if (null != ids && !ids.isEmpty()) {
                ResourceExample rExample = new ResourceExample();// 逻辑删除资源信息
                rExample.createCriteria().andIdIn(ids);
                Resource r = new Resource();
                r.setIsEffective(DBConst.FALSE);
                r.setCreateTime(new Date());
                resourceDao.updateByExampleSelective(r, rExample);
                RoleResourceExample example = new RoleResourceExample();// 物理删除资源角色关系
                example.createCriteria().andResourceIdIn(ids);
                code = roleResourceDao.deleteByExample(example);
                // 清除缓存
                List<Resource> resourceList = resourceDao.selectResourceList(resource);
                for (Resource temp : resourceList) {
                    ResourceCache.remove(temp.getUrl());
                }
            }
        	
        }
        
        return 0 <= code ? SUCCESS : FAILED;
    }

}